package cn.lyl.canting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.litepal.LitePal;

import java.util.List;

import cn.lyl.canting.R;
import cn.lyl.canting.bean.User;

public class PasswordActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar password_toolbar;
    private EditText ed_1;
    private EditText ed_2;
    private EditText ed_3;
    private Button btn_password;
    private String phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        initView();
    }

    private void initView() {
        password_toolbar = (Toolbar) findViewById(R.id.password_toolbar);
        ed_1 = (EditText) findViewById(R.id.ed_1);
        ed_2 = (EditText) findViewById(R.id.ed_2);
        ed_3 = (EditText) findViewById(R.id.ed_3);
        btn_password = (Button) findViewById(R.id.btn_password);

        setSupportActionBar(password_toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        password_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btn_password.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_password:
                //点击修改按钮进行修改验证
                    submit();
                    Intent intent = new Intent(this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                break;
        }
    }

    private void submit() {
        // validate
        User user = new User();

        //获取用户登录手机号
        Intent intent = getIntent();
        phone  =  intent.getStringExtra("phone");
        //Litepal查询phone列
        List<User> users = LitePal.where("phone = ?",phone).find(User.class);
        //获取用户原密码
        String password = users.get(0).getPassword();
        //用户输入的原密码
        String yuan_password = ed_1.getText().toString().trim();

        //判断是否为空
        if (TextUtils.isEmpty(yuan_password)) {
            Toast.makeText(this, "请输入原密码", Toast.LENGTH_SHORT).show();
            return;
        }else {
            //判断用户输的的原密码是否一致
            if(password.equals(yuan_password)){

            }else{
                Toast.makeText(this, "您输入的原密码有误", Toast.LENGTH_SHORT).show();
            }
        }
        //获取用户输入的新密码
        String xin_password_1 = ed_2.getText().toString().trim();
        if (TextUtils.isEmpty(xin_password_1)) {
            Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }
        //获取用户再次输入的新密码
        String  xin_password_2 = ed_3.getText().toString().trim();
        if (TextUtils.isEmpty(xin_password_2)) {
            Toast.makeText(this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }else{
            //判断两次输入是否一致
            if(xin_password_2.equals(xin_password_1)){
                //将新密码存入user中
                user.setPassword(xin_password_2);
                //LitePal修改User表中用户密码
                user.updateAll("phone = ?",phone);
            }else{
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            }
        }

        // TODO validate success, do something


    }
}
